FROM postgres:14.12

LABEL maintainer="codestation <vondser@163.com>"

ARG BACKREST_VERSION=2.54.2
ARG S6_OVERLAY_VERSION=3.2.0.2

# Install pgbackrest
RUN set -ex; \
	apt-get update; \
	DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
		ca-certificates \
		curl \
		pgbackrest=${BACKREST_VERSION}-* \
	; \
	rm -rf /var/lib/apt/lists/*;

# Create pgBackRest directories and configuration
RUN set -ex; \
	mkdir -p /var/lib/pgbackrest /var/log/pgbackrest /etc/pgbackrest /etc/pgbackrest/conf.d /tmp/pgbackrest; \
	touch /var/log/pgbackrest/pgbackrest.log; \
	chown -R postgres:postgres /var/lib/pgbackrest /var/log/pgbackrest /etc/pgbackrest /tmp/pgbackrest; \
	chmod 750 /var/lib/pgbackrest /var/log/pgbackrest /etc/pgbackrest /tmp/pgbackrest; \
	chmod 640 /var/log/pgbackrest/pgbackrest.log

# Copy pgBackRest configuration file
COPY pgbackrest.conf /etc/pgbackrest/
RUN chown postgres:postgres /etc/pgbackrest/pgbackrest.conf && chmod 640 /etc/pgbackrest/pgbackrest.conf

# Copy PostgreSQL custom configuration
COPY postgresql-custom.conf /etc/postgresql-custom.conf
RUN chown postgres:postgres /etc/postgresql-custom.conf && chmod 644 /etc/postgresql-custom.conf

# Create empty override configuration file
RUN touch /etc/postgresql-custom-override.conf && \
    chown postgres:postgres /etc/postgresql-custom-override.conf && \
    chmod 644 /etc/postgresql-custom-override.conf

# Add an initialization script to check and fix permissions on startup
RUN echo '#!/bin/bash\n\
mkdir -p /tmp/pgbackrest\n\
touch /var/log/pgbackrest/pgbackrest.log\n\
chown -R postgres:postgres /tmp/pgbackrest /var/lib/pgbackrest /var/log/pgbackrest\n\
chmod 750 /tmp/pgbackrest /var/lib/pgbackrest /var/log/pgbackrest\n\
chmod 640 /var/log/pgbackrest/pgbackrest.log\n\
exit 0' > /docker-entrypoint-initdb.d/01-fix-pgbackrest-permissions.sh && \
chmod +x /docker-entrypoint-initdb.d/01-fix-pgbackrest-permissions.sh

# Add initialization script to apply custom PostgreSQL configuration
RUN echo '#!/bin/bash\n\
mkdir -p /var/lib/postgresql/data/postgresql.conf.d\n\
cp /etc/postgresql-custom.conf /var/lib/postgresql/data/postgresql.conf.d/01-custom.conf\n\
cp /etc/postgresql-custom-override.conf /var/lib/postgresql/data/postgresql.conf.d/99-override.conf\n\
chown postgres:postgres /var/lib/postgresql/data/postgresql.conf.d/*.conf\n\
echo "include_dir = '\''postgresql.conf.d'\''" >> /var/lib/postgresql/data/postgresql.conf\n\
exit 0' > /docker-entrypoint-initdb.d/02-apply-custom-config.sh && \
chmod +x /docker-entrypoint-initdb.d/02-apply-custom-config.sh

# Install S6 Overlay
RUN set -ex; \
    curl -L https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-noarch.tar.xz -o /tmp/s6-overlay-noarch.tar.xz ; \
    tar -C / -Jxpf /tmp/s6-overlay-noarch.tar.xz ; \
    rm /tmp/s6-overlay-noarch.tar.xz ; \
    curl -L https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-$(arch).tar.xz -o /tmp/s6-overlay-$(arch).tar.xz ; \
    tar -C / -Jxpf /tmp/s6-overlay-$(arch).tar.xz ; \
    rm /tmp/s6-overlay-$(arch).tar.xz

COPY services/ /etc/s6-overlay/s6-rc.d/

ENTRYPOINT ["/init"]
